package com.wufan.day03;

/**
 * @author SongWufan
 * @date 2021/3/7 16:05
 */
public class StudentTest {
    public static void main(String[] args) {
        Student student = new Student();
        //声明Student类型的数组
        Student[] students = new Student[20];
        for (int i = 0; i < students.length; i++) {
            //给数组元素赋值
            students[i] = new Student();
            //给属性赋值
            students[i].number = i + 1;
            students[i].state = (int) (Math.random() * 6 + 1);
            students[i].score = (int) (Math.random() * 101);
        }
        //排序
        student.sort(students);
        //遍历数组
        student.print(students);
        //三年级学生
        student.searchState(students,4);
    }
}

class Student {
    /**
     * 学号，年级，成绩
     */
    int number;
    int state;
    int score;

    public String info() {
        return number + "\t" + state + "\t" + score;
    }

    public void print(Student[] students) {
        System.out.println("学号" + "\t" + "年级" + "\t" + "成绩");
        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i].info());
        }
    }

    /**
     * 查找年级学生信息
     * @param students 学生数组
     * @param state 年级
     */
    public void searchState(Student[] students, int state) {
        System.out.println("---"+state+"年级---");
        for (int i = 0; i < students.length; i++) {
            if (students[i].state == 3) {
                System.out.println(students[i].info());
            }
        }
    }

    /**
     * 排序
     * @param students
     */
    public void sort(Student[] students){
        System.out.println("---排序---");
        for (int i = 0; i < students.length - 1; i++) {
            for (int j = 0; j < students.length - 1 - i; j++) {
                if (students[j].score > students[j + 1].score) {
                    Student temp = students[j];
                    students[j] = students[j + 1];
                    students[j + 1] = temp;
                }
            }
        }
    }
}
